package com.example.alarmclock;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Arrays;
import java.util.Calendar;

public class AlarmDetailActivity extends AppCompatActivity {

    private TimePicker timePicker;
    private EditText labelEditText;
    private Spinner repeatSpinner;
    private CheckBox vibrationCheckBox;
    private Button saveButton;
    private AlarmModel alarm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_detail);

        timePicker = findViewById(R.id.time_picker);
        labelEditText = findViewById(R.id.label_edit_text);
        repeatSpinner = findViewById(R.id.repeat_spinner);
        vibrationCheckBox = findViewById(R.id.vibration_check_box);
        saveButton = findViewById(R.id.save_button);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.repeat_options_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        repeatSpinner.setAdapter(adapter);

        Intent intent = getIntent();
        if (intent.hasExtra("alarm")) {
            String alarmInfo = intent.getStringExtra("alarm");
            alarm = AlarmModel.fromString(alarmInfo);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(alarm.getTime());

            timePicker.setHour(calendar.get(Calendar.HOUR_OF_DAY));
            timePicker.setMinute(calendar.get(Calendar.MINUTE));

            labelEditText.setText(alarm.getLabel());

            int repeatIndex = Arrays.asList(getResources().getStringArray(R.array.repeat_options_array)).indexOf(alarm.getRepeat());
            repeatSpinner.setSelection(repeatIndex);

            vibrationCheckBox.setChecked(alarm.isVibrate());
        } else {
            alarm = new AlarmModel();
        }

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int hour = timePicker.getHour();
                int minute = timePicker.getMinute();
                String label = labelEditText.getText().toString();
                String repeat = repeatSpinner.getSelectedItem().toString();
                boolean vibrate = vibrationCheckBox.isChecked();

                alarm.setHour(hour);
                alarm.setMinute(minute);
                alarm.setLabel(label);
                alarm.setRepeat(repeat);
                alarm.setVibrate(vibrate);

                Intent resultIntent = new Intent();
                resultIntent.putExtra("alarm_info", alarm.toString());
                setResult(RESULT_OK, resultIntent);
                finish();
            }
        });
    }
}



